<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2020/1/14
 * Time: 14:32
 */

namespace app\api\controller\common;
use app\api\model\AccessToken as AccessTokenModel;


class AccessToken
{
    private  $access_token = null; //token
    private $uid = null;//待生成token的uid

    public function __construct($uid)
    {
        $this->uid = $uid;
        $this->createToken();//生成
        $this->saveToken();//存储
    }

    private function createToken(){ //生成token 算法
        $this->access_token =  md5('JG_2020'.md5(time().mt_rand(10000,99999)));
    }

    private function saveToken(){//存储token
        $ak = new AccessTokenModel([
            'token'=>$this->access_token,
            'uid'=>$this->uid,
            'expires'=>7200
        ]);
        $ak->save();
    }

    public function getToken(){//对外提供获取token
        return $this->access_token;
    }
}